// pages/classicCaseDetail/classicCaseDetail.js
const util = require('../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
		/* 机器人移动的位置 */
		leftLooks: 250,
		topLooks: 350,
		classicCaseList:[],
		/* 选择的tab下标 */
		currIndex:0,
		/* 类别id */
		childCategory:"",
		/* 页面id */
		parentCategory:"",
		/* 分类列表 */
		categoryList:[],
		/* 页码 */
		page:1,
		/* 底部显示无更多数据 */
		noneData:false,
		/* 列表margin */
		MarginTop:0,
		/* 列表高度 */
		Height:0,
		newHeight:0,
		/* 手机宽高 */
		phoneHeight:0,
		phoneWidth:0,
		/* 是否下拉刷新 */
		refresh:false,
		/* 搜索内容 */
		inputValue:"",
		/* 创建多个自定义组件 */
		pages:[],
		/* 当前页面的page值 */
		pageLength:0,
		/* 最多页码 */
		maxPage:2,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
		wx.setNavigationBarTitle({ title: options.name });
		this.data.parentCategory=options.id;
		wx.setStorageSync('maxPage',1);
		wx.showShareMenu({  
			withShareTicket:true,
			menus: ['shareAppMessage', 'shareTimeline']
		})
  },
	/* 搜索内容 */
	searchValue:function(e){
		this.setData({
			inputValue:e.detail.value
		})
	},
	/* 搜索列表 */
	searchContent:function(){
		this.setData({
			pages:[],
			pageLength:0,
		})
		wx.setStorageSync('maxPage',1);
		this.queryArticle();
	},
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
		var _this=this;
		/* 获取手机可视区域宽高 */
		wx.getSystemInfo({
			success: function(res) {
				_this.data.phoneWidth=res.windowWidth;
				_this.data.phoneHeight=res.windowHeight;
			},
		});
		wx.createSelectorQuery().selectAll('.search-fixed').boundingClientRect(function (rect) {
			console.log(_this.data.phoneHeight);
			_this.setData({
				MarginTop:rect[0].height+10,
				Height:_this.data.phoneHeight-rect[0].height-10,
				newHeight:_this.data.phoneHeight-rect[0].height-10
			})
			
		}).exec();
		this.queryDataByParentId();
  },
	/* 筛选tab内容 */
	filterFn:function(e){
		if(this.data.currIndex!=e.currentTarget.dataset.index){
			this.setData({
				currIndex:e.currentTarget.dataset.index,
				childCategory:e.currentTarget.dataset.category,
				noneData:false,
				pages:[],
				pageLength:0,
			})
			wx.setStorageSync('maxPage',1);
			this.queryArticle();
		}
		
	},
	/* 跳转到问题页面 */
	toQuestion:function(){
		wx.navigateTo({
			url:"../questions/questions"
		})
	},
	/* 查询分类Id */
	queryDataByParentId:function(){
		var _this=this;
		/* 查地区 */
		wx.request({
			url: util.url+"/system/queryDataByParentId/"+_this.data.parentCategory,
			data: {},
			header: {
				'content-type': 'application/json' 
			},
			success(res) {
				if(res.data.resultCode==0){
					if(res.data.resultData.length){
						_this.setData({
							categoryList:res.data.resultData,
							childCategory:res.data.resultData[0].id
						});
					}
					_this.queryArticle();
				}else{
					wx.showToast({
						icon:"none",
						title:res.data.resultMsg
					})
				}
			}
		});
	},
	/* 查询最新动态 */
	queryArticle:function(){
		var _this=this;
		if(wx.getStorageSync('maxPage')>this.data.pageLength){
			this.data.pageLength+=1;
			this.data.pages.push(this.data.pageLength);
			this.setData({
				parentCategory:this.data.parentCategory,
				childCategory:this.data.childCategory+"",
				type: "12",
				pages:this.data.pages,
				orderBy:"publish_at",
				searchContent:this.data.inputValue,
				status:"2",
			})
			/* 保存最新的最大页码数 */
			var timer=setInterval(function(){
				if(wx.getStorageSync('newPage')){
					_this.data.maxPage=wx.getStorageSync('maxPage');
					wx.setStorageSync('newPage',false);
					clearInterval(timer);
				}
			},500);
		}else{
			this.setData({
				noneData:true
			});
			wx.createSelectorQuery().selectAll('.none-data').boundingClientRect(function (rect) {
				this.setData({
					newHeight:this.data.Height-rect[0].height
				})
			}).exec();
		}
	},
 
	/* 滑动机器人 */
	imagetouchmove:function(e){
		var phoneWidth=this.data.phoneWidth;
		var phoneHeight=this.data.phoneHeight;
		var robotWidth=wx.getStorageSync("robotWidth");
		var robotHeight=wx.getStorageSync("robotHeight");
		e.touches[0].clientX=e.touches[0].clientX<0?0:e.touches[0].clientX;
		e.touches[0].clientY=e.touches[0].clientY<0?0:e.touches[0].clientY;
		e.touches[0].clientX=e.touches[0].clientX>phoneWidth-robotWidth?phoneWidth-robotWidth:e.touches[0].clientX;
		e.touches[0].clientY=e.touches[0].clientY>phoneHeight-robotHeight?phoneHeight-robotHeight:e.touches[0].clientY;
		
    this.setData({
      leftLooks: e.touches[0].clientX,
      topLooks: e.touches[0].clientY
    })
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
		if(!this.data.noneData){
			wx.setStorageSync('newPage',false);
			if(this.data.maxPage>-1){
				wx.setStorageSync('maxPage',this.data.maxPage);
			}
			this.queryArticle();
		}
  },

  /**
  * 页面相关事件处理函数--监听用户下拉动作
  */
  onPullDownRefresh: function () {
		this.setData({
			pages:[],
			pageLength:0,
			refresh:true,
		})
		wx.setStorageSync('maxPage',1);
  	this.queryArticle();
  	wx.showNavigationBarLoading() //在标题栏中显示加载
  },
	/**
		 * 用户点击右上角分享朋友
		 */
		onShareAppMessage: function (e) {
			
		},
		/**
		* 用户点击右上角分享朋友圈
		*/
		onShareTimeline: function () {
	
		 },
})